home *** CD-ROM | disk | FTP | other *** search
- Path: news.bridge.net!news
- From: psycho@bridge.net (Gary Thompson)
- Newsgroups: comp.lang.c
- Subject: Re: NEwbie: How to return a multi-dimensional array from function?
- Date: Fri, 15 Mar 1996 17:16:49 GMT
- Organization: BridgenetLC - 305.374.3031 - 100 S. Biscayne Blvd, Miami
- Message-ID: <4ic8pj$c99@news.bridge.net>
- References: <4hp273$8bu@news.xs4all.nl> <31404CE9.1A4A@mc.net> <mjs.826298629@hubcap> <4ib78s$6gv@news.bridge.net> <4ic3p3$cl5@news.xs4all.nl>
- NNTP-Posting-Host: ppp-ftl1-19.bridge.net
- X-Newsreader: Forte Free Agent 1.0.81
-
- falstaff@xs4all.nl (Falstaff) wrote:
-
- >psycho@bridge.net (Gary Thompson) writes:
-
- >>>Two things: (1) If you are going to use the value of tmp outside of Foo(),
- >>>then you need to declare
- >>> static tmp[10][5];
- >>>Otherwise the storage may disappear when you return from Foo().
-
- >>No, declaring it as static will only retain the value in the foo() function, if
- >>you call it more than once. You CANNOT use a value declared in a subfunction in
- >>MAIN. You would have to remove both TMP declarations from MAIN and FOO and
- >>declare the thing globally outside of main.
-
- >WRONG!!!
-
- >If you declare a function
-
- >int *demo(void)
- >{ static int n=0;
- > n++;
- > return &n;
- >}
-
- >the caller can use the static variable until the next time your function
- >is called. This is how the standard library function tmpnam() returns,
- >for example.
-
- Well, of course it will work in your example... you returned the value. Try
- this...
-
- main()
- {
- foo();
- n++;
- }
-
- foo()
- {
- static int n=0;
- n=1;
- }
-
- This will give you a compile error. You cannot use N outside of foo() which was
- the point I was making. However, if you called foo again, N will be 1 before it
- get's re-assigned to 1.
-
- Gary Thompson
- "The Psycho"
- psycho@bridge.net
- 72607.1365@compuserve.com
- http://www.bridge.net/~psycho
- HTTP://ourworld.compuserve.com/homepages/psychotps
-
-